package com.roncoo.education.course.service.api.auth;

import com.auth0.jwt.interfaces.DecodedJWT;
import com.roncoo.education.course.common.req.CourseVideoMemoryREQ;
import com.roncoo.education.course.service.api.auth.biz.AuthApiCourseBiz;
import com.roncoo.education.course.service.api.auth.biz.AuthApiCourseVideoPeriodMemoryBiz;
import com.roncoo.education.course.service.dao.impl.mapper.entity.CoursePeriodMemory;
import com.roncoo.education.util.tools.JWTUtil;

import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import com.roncoo.education.course.common.bo.auth.AuthCourseSignBO;
import com.roncoo.education.course.common.bo.auth.AuthCourseViewBO;
import com.roncoo.education.course.common.dto.auth.AuthCourseSignDTO;
import com.roncoo.education.course.common.dto.auth.AuthCourseViewDTO;
import com.roncoo.education.util.base.BaseController;
import com.roncoo.education.util.base.Result;

import io.swagger.annotations.ApiOperation;

import java.io.UnsupportedEncodingException;

/**
 * 课程信息-审核
 *
 * @author wujing
 */
@RestController
@RequestMapping(value = "/course/auth/course")
@Api(description = "课程信息-审核")
public class AuthApiCourseController extends BaseController {

	@Autowired
	private AuthApiCourseBiz biz;

	@Autowired
	private AuthApiCourseVideoPeriodMemoryBiz videoPeriodMemoryBiz;

	/**
	 * 课程详情接口
	 */
	@ApiOperation(value = "课程详情接口", notes = "用户登录后获取课程详情信息接口")
	@RequestMapping(value = "/view", method = RequestMethod.POST)
	public Result<AuthCourseViewDTO> view(@RequestBody AuthCourseViewBO authCourseViewBO) {
		return biz.view(authCourseViewBO);
	}

	/**
	 * 课时播放获取sign值接口
	 */
	@ApiOperation(value = "课时播放获取sign值接口", notes = "课时播放获取sign值接口")
	@RequestMapping(value = "/sign", method = RequestMethod.POST)
	public Result<AuthCourseSignDTO> sign(@RequestBody AuthCourseSignBO authCourseSignBO) {
		return biz.sign(authCourseSignBO);
	}

	/**
	 *Updated upstream
	 * 获取视频上次播放进度
	 * @param token 用户token
	 * @param videoNo 视频编号
	 * @return
	 */
	@ApiOperation("获取视频上次播放进度")
	@RequestMapping(value = "videoLastMemory",method = RequestMethod.POST)
	public Result<CoursePeriodMemory> videoLastMemory(@RequestHeader("token")String token, @RequestParam("videoNo")Long videoNo){
		Long userNo = null;
		try {
			DecodedJWT verify = JWTUtil.verify(token);
			userNo = JWTUtil.getUserNo(verify);
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
			return Result.error("解析token失败");
		}

		CoursePeriodMemory byVideoNoAndUserNo = videoPeriodMemoryBiz.getByVideoNoAndUserNo(videoNo, userNo);
		return Result.success(byVideoNoAndUserNo);
	}

	/**
	 * 更新视频播放进度
	 * @param req 进度对象
	 * @param token 用户token
	 * @return
	 */
	@ApiOperation("更新视频播放进度")
	@RequestMapping(value = "updateVideoMemory",method = RequestMethod.POST)
	public Result updateVideoMemory(@RequestBody @Validated CourseVideoMemoryREQ req, @RequestHeader("token")String token){
		//感觉应该是放缓存中，异步更新数据库数据，目前先数据库为主
		Long userNo = null;
		try {
			DecodedJWT verify = JWTUtil.verify(token);
			userNo = JWTUtil.getUserNo(verify);
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
			return Result.error("解析token失败");
		}

		return videoPeriodMemoryBiz.updateUserVideoMemory(req,userNo);

	}


}
